package com.yyx.lgb.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.yyx.lgb.common.exception.LgbException;
import com.yyx.lgb.manager.listener.ExcelListener;
import com.yyx.lgb.manager.mapper.CategoryMapper;
import com.yyx.lgb.manager.service.CategoryService;
import com.yyx.lgb.model.entity.product.Category;
import com.yyx.lgb.model.vo.common.ResultCodeEnum;
import com.yyx.lgb.model.vo.product.CategoryExcelVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: CategoryServiceImpl
 * Package: com.yyx.lgb.manager.service.impl
 * Description:
 *
 * @Author: yyx
 * @Create: 2025/8/6 - 10:10
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findByParentId(Long parentId) {
        List<Category> list =categoryMapper.findByParentId(parentId);
        if(!CollectionUtils.isEmpty(list)){
            //遍历分类集合 获取每一个分类数据
            list.forEach(item ->{
              int count=  categoryMapper.countByParentId(item.getId());
              if(count>0){
                  item.setHasChildren(true);
              }
              else {
                  item.setHasChildren(false);
              }

            });
        }
        return list;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {

            // 设置响应结果类型       ms-excel 微软中的表格
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");

            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("分类数据", "UTF-8");
            // 设置响应头信息  Content-disposition （必须要有   以下载的方式打开头信息）
            // （值名称也是固定的："attachment;filename="）
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");

            // 查询数据库中的数据
            List<Category> categoryList = categoryMapper.selectAll();
            List<CategoryExcelVo> categoryExcelVoList = new ArrayList<>(categoryList.size());

            // 将从数据库中查询到的Category对象转换成CategoryExcelVo对象
            for(Category category : categoryList) {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtils.copyProperties(category, categoryExcelVo, CategoryExcelVo.class);
                categoryExcelVoList.add(categoryExcelVo);
            }

            // 写出数据到浏览器端
            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class).sheet("分类数据").doWrite(categoryExcelVoList);

        } catch (IOException e) {
            e.printStackTrace();
            throw new LgbException(ResultCodeEnum.DATA_ERROR);
        }
    }

    @Override
    public void importData(MultipartFile file) {

        try {
            //创建监听
            ExcelListener<CategoryExcelVo>  excelListener=new ExcelListener<>(categoryMapper);
            //调用read
            EasyExcel.read(file.getInputStream(),CategoryExcelVo.class,excelListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new LgbException(ResultCodeEnum.DATA_ERROR);
        }
    }
}
